package com.reds.backend.service.impl.address;
import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reds.backend.mapper.addressMapper;
import com.reds.backend.pojo.Address;
import com.reds.backend.pojo.Article;
import com.reds.backend.pojo.Homearticle;
import com.reds.backend.service.address.addressCRUDService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.reds.backend.mapper.articleMapper;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class addressCRUDServiceImp implements addressCRUDService {
    @Autowired
        addressMapper addressMapper;
    @Autowired
        articleMapper articleMapper;

    @Override
    public String addAddress(int aid, String address) {
        Address address1 = new Address();
        address1.setAid(aid);
        address1.setAddress(address);
        System.out.println(address1);
        addressMapper.insert(address1);
        return "success";
    }

    @Override
    public String deteleAddress(int id) {
        addressMapper.deleteById(id);
        return "success";
    }

    @Override
    public String updataAddress(int aid,String address) {
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("aid",aid);
        Address address1 = addressMapper.selectOne(queryWrapper);
        address1.setAddress(address);
        addressMapper.updateById(address1);
        System.out.println(address1);
        return "success";

    }

    @Override
    public String getAddress(int aid) {
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("aid",aid);
        Address address = addressMapper.selectOne(queryWrapper);
        return address.getAddress();
    }

    @Override
    public JSONObject getArticleByAid(String address,int page,int sizes) {
        class withAdd{
            public int id;
            public String title;
            public String content;
            public String author;
            public Date time;
            public String face;
            public String address;

            void setArticle(Article article){
                this.id = article.getId();
                this.title =article.getTitle();
                this.content = article.getContent();
                this.author = article.getAuthor();
                this.time = article.getTime();
                this.face = article.getFace();
            }
        }

        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("address",address).orderByDesc("id");
        IPage<Address> iPage = new Page<>(page,sizes);
        List<Address> list  = addressMapper.selectPage(iPage,queryWrapper).getRecords();
        JSONObject jsonObject = new JSONObject();
        List<withAdd> withAddList = new ArrayList<>();
        System.out.println(list);
        for(Address address1 : list){
            Article article = articleMapper.selectById(address1.getAid());
            if(article == null) continue;
            System.out.println(article);
            withAdd withAdd = new withAdd();
            withAdd.setArticle(article);
            withAdd.address = address1.getAddress();
            withAddList.add(withAdd);
        }
        jsonObject.put("article",withAddList);
        jsonObject.put("length",addressMapper.selectCount(queryWrapper));
        return jsonObject;

    }
}

